﻿using System;
using System.IO;
using EmbeddedSharp.Core;

namespace EmbeddedSharp.Scripting.Function.IO {

    /// <summary>
    /// CreateFile("c:\\hello.txt");
    /// </summary>
    public class OpenFileNode : FuncNode {

        public OpenFileNode()
            : base() {
            base.Value = "openFile";
        }

        public override object Evaluate() {
            if (Parameters.Count < 1) {
                return "usage: openFile(fileName[, open mode])";
            }

            try {
                string fileName = (string) Parameters[0].Evaluate();
                FileStream fs = File.Create(fileName);
                fs.Close();
                return fileName;
            } catch (Exception ex) {
                return ex.Message;
            }

            return "file cannot be created.";
        }
    }
}
